<template>
  <div id="globalHeader">
    <!--:wrap="false" -> 响应式，取消分辨率变小时自动换行-->
    <a-row :wrap="false">
      <a-col flex="170px">
        <div class="title-bar">
          <img src="../assets/logo.png" alt="logo" class="logo">
          <div class="title">用户中心</div>
        </div>
      </a-col>
      <a-col flex="auto">
        <a-menu
            v-model:selectedKeys="current"
            mode="horizontal"
            :items="items"
            @click="doMenuClick"
        />
      </a-col>
      <a-col flex="80px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.userId">
            {{ loginUserStore.loginUser.userName ?? '未命名'}}
          </div>
          <div v-else>
            <a-button type="primary" @click="doLoginBtuClick()">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>

</template>
<script lang="ts" setup>
import { h, ref } from 'vue';
import {
  HomeOutlined,
  AppstoreOutlined,
  SettingOutlined,
  CrownOutlined
} from '@ant-design/icons-vue';
import { MenuProps } from 'ant-design-vue';
import { useRouter } from "vue-router";
import { useLoginStore } from "@/store/useLoginUserStore";

const loginUserStore = useLoginStore();


const router = useRouter();
// 路由跳转事件
const doMenuClick = ({ key }: { key: string }) => {
  router.push({ path: key });
}

// 导航栏当前值（导航栏高亮显示当前菜单）
const current = ref<string[]>(['mail']);
router.afterEach((to) => {
  current.value = [to.path]
});


// 登录按钮跳转登录页
const doLoginBtuClick = () => {
  router.push(
      {
        path: '/user/login'
      }
  );
}

// 菜单项
const items = ref<MenuProps['items']>([
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/user/login',
    label: '用户登陆',
    title: '用户登陆',
  },
  {
    key: '/user/register',
    icon: () => h(AppstoreOutlined),
    label: '用户注册',
    title: '用户注册',
  },
  {
    key: '/user/manager',
    icon: () => h(CrownOutlined),
    label: '用户管理',
    title: '用户管理',
  },
  {
    key: '/user/other',
    label: '其他',
    title: '其他',
    children: [
      {
        type: 'group',
        label: '其他 1',
        children: [
          {
            label: '孩子 1',
            key: 'setting:1',
          },
          {
            label: '孩子 2',
            key: 'setting:2',
          },
        ],
      },
    ],
  },
  {
    key: '/user/jump',
    label: h('a', { href: 'http://localhost:8080', target: '_blank' }, '跳转'),
    title: '外链',
  },
  {
    key: '/user/setting',
    icon: () => h(SettingOutlined),
    label: '设置',
    title: '设置',
  },
]);
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center; /*垂直居中*/
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 48px;
}
</style>


